package com.genuine.vip;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.genuine.hyjj.AssistUser;
import com.genuine.hyjj.R;
import com.genuine.net.ApacheHttpConnect;

public class GetVipActivity extends Activity{
	private String uid = "x";
	private String barcode;
	private TextView infoview;
	private Button getcardbtn;
	private Button cancelbtn;
	private ProgressBar myprogressBar;
	private ProgressDialog dialog=null;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.getvipcard);
        
		Intent theIntent = getIntent();
		barcode = theIntent.getStringExtra("barcode");
		uid=AssistUser.getInstance(this).getId();
		Log.d("demo", barcode);
		
		getcardbtn = (Button)findViewById(R.id.getbtn);
		cancelbtn = (Button)findViewById(R.id.cancelbtn);
		infoview = (TextView)findViewById(R.id.cardinfo);
		myprogressBar = (ProgressBar)findViewById(R.id.myprogressBar);
		
		GetCardInfoTask getinfotask = new GetCardInfoTask();
		getinfotask.execute(barcode);
		
		getcardbtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				GetCardTask getcardtask = new GetCardTask();
				getcardtask.execute(barcode, uid);
			}
		});
		
		cancelbtn.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	class GetCardTask extends AsyncTask<String, Integer, String> {
		protected void onPostExecute(String result) {
			dialog.dismiss();
			if (result.equals("-1")) {
				Log.d("demo", "参数错误！");
			} else if (result.equals("0")) {
				Toast.makeText(GetVipActivity.this, "领取失败,请重试!", Toast.LENGTH_SHORT).show();
			} else if (result.equals("1")) {
				Toast.makeText(GetVipActivity.this, "会员卡领取成功，请到‘我的会员卡里‘查询!", Toast.LENGTH_LONG).show();
			} else {
				Log.d("demo", "服务器错误！");
			}
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog=ProgressDialog.show(GetVipActivity.this, "领取会员卡", "正在领取...");
			dialog.show();
		}
		protected String doInBackground(String... params) {
			String url = "http://www.ypdm.com/Handler/Vip/GetVipCard.ashx?barcode="
					+ params[0] + "&uid=" +params[1];
			
			String result = null;
			try {
				result = ApacheHttpConnect.getContent(url);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Log.d("demo", result);
			return result.trim();
		}
	}
	
	class GetCardInfoTask extends AsyncTask<String, Integer, String> {
		protected void onPostExecute(String result) {
			myprogressBar.setVisibility(View.GONE);
			if (result.equals("-1")) {
				Log.d("demo", "参数错误！");
			} else {
				try {
					String msg = null;
					JSONArray objs = new JSONArray(result.trim());
					JSONObject obj = (JSONObject) objs.get(0);
					Log.d("demo", "0");
					msg = "厂商名: "
						+ obj.getString("FactoryName")
						+ "\n产品名: "
						+ obj.getString("CommodityName")
						+ "\n许可: "
						+ obj.getString("Permition")
						+ "\n优惠详情: "
						+ obj.getString("RuleDetail")
						+ "\n生效时间: "
						+ obj.getString("BeginDate")
						+ "\n失效时间: "
						+ obj.getString("EndDate");
					Log.d("demo", "1");
					infoview.setVisibility(View.VISIBLE);
					infoview.setText(msg);
				} catch (JSONException e) {
					Log.d("demo", "服务器错误！");
					e.printStackTrace();
				}
			}
		}
		
		protected String doInBackground(String... params) {
			String url = "http://www.ypdm.com/Handler/Vip/GetVipCardInfo.ashx?barcode="
					+ params[0];
			
			Log.d("demo", url);
			
			String result = null;
			try {
				result = ApacheHttpConnect.getContent(url);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Log.d("demo", result);
			return result.trim();
		}
	}
}